package com;

import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class Collection_16_SortedMap_02 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		SortedMap products = new TreeMap();

		Product2 p1 = new Product2("water", 1.0);
		Product2 p2 = new Product2("苹果", 3.0);
		Product2 p3 = new Product2("香蕉", 2.0);
		Product2 p4 = new Product2("梨", 1.5);

		// value 可以设置为 已购买的数量
		products.put(p1, 4.0);
		products.put(p2, 2.0);
		products.put(p3, 1.0);
		products.put(p4, 4.0);

		Set keys = products.keySet();
		for (Object key : keys) {
			Product2 p = (Product2) key;
			double value = (double) products.get(key);
			System.out.println(p + "--->" + value + "kg 总价 = " + (value * p.price));
		}

	}
}

class Product2 implements Comparable {
	String name;
	double price;

	public Product2(String name, double price) {
		super();
		this.name = name;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Product [name=" + name + ", price=" + price + "]";
	}

	@Override
	public int compareTo(Object o) {
		double price1 = this.price;
		double price2 = ((Product2) o).price;
		if (price1 == price2) {
			return 0;
		} else if (price1 < price2) {
			return -1;
		} else {
			return 1;
		}
	}

}